package com.adam.blog.controller;

import com.adam.blog.common.aop.LogAnnotation;
import com.adam.blog.common.cache.Cache;
import com.adam.blog.dao.pojo.Article;
import com.adam.blog.service.ArticleService;
import com.adam.blog.vo.ArticleVo;
import com.adam.blog.vo.params.ArticleParams;
import com.adam.blog.vo.params.PageParams;
import com.adam.blog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//都返回json数据
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    @PostMapping
    //加上此自定义注解 代表要对此接口记录日志  AOP日志记录
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5 * 60 * 1000,name = "list_article")    //自定义Aop 统一缓存处理
    public Result listArticle(@RequestBody PageParams pageParams){

        return Result.success(articleService.listArticle(pageParams));
    }

    /**
     * 最热文章列表
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit = 5;
        List<ArticleVo> articleList = articleService.hotArticle(limit);
        return Result.success(articleList);
    }

    /**
     * 最新文章列表
     * @return
     */
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000,name = "new_article")
    public Result newArticle(){
        int limit = 5;
        List<ArticleVo> articleList = articleService.newArticle(limit);
        return Result.success(articleList);
    }

    /**
     * 文章归纳
     * @return
     */
    @PostMapping("listArchives")
    @Cache(expire = 5 * 60 * 1000,name = "list_archives")
    public Result listArchives(){

        return articleService.listArchives();
    }

    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long id){
        System.out.println(id);
        return articleService.findArticleById(id);
    }

    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParams articleParams){

        return articleService.publish(articleParams);
    }

}
